<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if (isset($is_guest))
{
    header("location: $PHP_SELF?i=2");
    exit;
}

//-----------------------------------------------------------------
//  Find user in the database
//-----------------------------------------------------------------

$user = $db->getLine("SELECT * FROM users WHERE id='{$_SESSION['id']}' ".
                     "AND username='{$_SESSION['username']}'");

//-----------------------------------------------------------------
//  View Profile
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Show profile case
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Code for delete avatar button
		//-----------------------------------------------------------------

		if(isset($user['avatar']))
			$del = "<a href='$PHP_SELF?n=modules/panel&a=4&d={$user['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' width='18' height='18' alt='{$language['Profile avatar desc2']}'></a>";
		else
			$del = "<img src='templates/$template/images/buttons/nodelete.gif' border='0' width='18' height='18' alt='{$language['Profile avatar desc']}'>";

		//-----------------------------------------------------------------
		//  Display profile form
		//-----------------------------------------------------------------

		?>
			<form action="<?= $PHP_SELF ?>?n=modules/panel" method="post" name="profile">
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<tr class='head'><td colspan='2'><?= $language['Profile view title'] ?></td></tr>
			<tr class="bottom">
			<td height="21" colspan="2"><?= $language['Profile view title2'] ?></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc'] ?></td>
			<td class="con1"><input type="password" style="width:100%" maxlength="25" class="box" name="editpassword"></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc2'] ?></td>
			<td class="con1"><input type="password" style="width:100%" maxlength="25" class="box" name="editpassword2"></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc3'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['email'] ?>" class="box" name="editemail"></td>
			</tr>
			<tr class="bottom">
			<td height="21" colspan="2"><?= $language['Profile view title3'] ?></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc4'] ?></td>
			<td class="con1"><input style="width:100%" maxlength="35" value="<?= $user['url'] ?>" class="box" name="editurl"></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc5'] ?>
		<?

		if(isset($o))
			echo "<a href='$PHP_SELF?n=modules/panel&a=1'> {$language['Profile view desc6']}</a></td>";
		else
			echo "<a href='$PHP_SELF?n=modules/panel&a=1&o=1'> {$language['Profile view desc7']}</a></td>";
 
		echo "<td class='con1'>";

		if(isset($o))
			echo "<select style='width:100%' maxlength='25' class='box' name='editflag'>".find_country($user['flag'])."</select>";
		else
			echo "<input style='width:100%' maxlength='25' value='{$user['location']}' class='box' name='editlocation'>" ?>
			</td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc8'] ?></td>
			<td class="con1"><table cellspacing="0" cellpadding="0" width="100%"><tr class='empty'><td width='30%'>
			<select name="editday" class="box" style="width:100%">
		<?

		if (!$user['age'])
			echo "<option value='0' selected></option>";
		else 
			echo "<option value='0'></option>";

		for ($i = 1; $i <= 31; $i++)
		{
			if (isset($user['age']))
			{
				if ($i == date("j", $user['age']))
                    $selected = "selected";
				else
                    $selected = "";
			}
            
            echo "<option value='$i' $selected>$i</option>\n";
        }
        
        echo
        "</select></td>".
        "<td align='center' width='5%'>-</td><td width='30%'>".
        "<select name='editmonth' class='box' style='width:100%'>";
        
        if(!$user['age'])
			echo "<option value='0' selected></option>";
		else 
			echo "<option value='0'></option>";

		for ($i = 1; $i <= 12; $i++)
		{
			if (isset($user['age']))
			{
				if ($i == date("n",$user['age']))
                    $selected = "selected";
				else
                    $selected = "";
			}

			echo "<option value='$i' $selected>{$months[$i]}</option>";
		}
        
        echo
        "</select></td>".
        "<td align='center' width='5%'>-</td><td width='30%'>".
        "<select name='edityear' class='box' style='width:100%'>";

		if(!$user['age'])
			echo "<option value='0' selected></option>";
		else 
			echo "<option value='0'></option>";

		for ($i = date("Y") - 5; $i >= 1970; $i--)
		{
			if (isset($user['age']))
			{
				if($i == date("Y",$user['age']))
                    $selected = "selected";
				else
                    $selected = "";
			}

			echo "<option value='$i' $selected>$i</option>\n";
		}

		?>
			</select></td></tr></table>
			</td>
			</tr>
			<tr>
			<td class="con1" valign="middle"><?= $language['Profile view desc9'] ?></td>
			<td class="con1" valign="bottom"><input type="radio" name="edithideemail" value="1" <? if ($user['hideemail'] == "1") echo "checked" ?>>&nbsp;<?= $language['Profile view desc21'] ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="edithideemail" value="0" <? if ($user['hideemail'] == "0") echo "checked" ?>>&nbsp;<?= $language['Profile view desc22'] ?></td>
			</tr>
			<tr>
			<td class="con1" valign="middle"><?= $language['Profile view desc10'] ?></td>
			<td class="con1" valign="bottom"><input type="radio" name="editcalendarbday" value="1" <? if ($user['calendarbday'] == "1") echo "checked" ?>>&nbsp;<?= $language['Profile view desc21'] ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="editcalendarbday" value="0" <? if ($user['calendarbday'] == "0") echo "checked" ?>>&nbsp;<?= $language['Profile view desc22'] ?></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc11'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['msn'] ?>" class="box" name="editmsn"></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc12'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['yahoo'] ?>" class="box" name="edityahoo"></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc13'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['icq'] ?>" class="box" name="editicq"></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc14'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['aim'] ?>" class="box" name="editaim"></td>
			</tr>
			<tr>
			<td class="con1" width="50%"><?= $language['Profile view desc15'] ?></td>
			<td class="con1" width="50%"><input style="width:100%" maxlength="35" value="<?= $user['skype'] ?>" class="box" name="editskype"></td>
			</tr>
			<tr>
			<td class="con1" width="50%" valign="top"><?= "{$language['Profile view desc16']}".dis_bb_em()."" ?></td>
			<td class="con1" width="50%"><textarea name="editsignature" style="width:100%; height:70px;" class="box"><?= $user['signature'] ?></textarea></td>
			</tr>
			<tr>
			<td class="con1" valign="top"><?= "{$language['Profile view desc19']}".dis_bb_em()."" ?></td>
			<td class="con1" valign="bottom"><textarea name="editaboutme" style="width:100%; height:70px;" class="box"><?= $user['aboutme'] ?></textarea></td>
			</tr>					
			<tr class="bottom">
			<td height="21" colspan="2"><?= $language['Profile view title5'] ?></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Profile view desc20'] ?></td>
			<td class="con1"><input type="submit" style="width:120" class="box" value="<?= $language['Profile save'] ?>"></td>
			</tr>
			<tr class="bottom">
			<td colspan="2"><?= $language['Profile avatar title'] ?></td>
			</tr>
			<tr>
			<td class="con1" colspan="2">
			<table width="100%" border="0" cellspacing='0' cellpadding='0'>
			<col width="15%"><col width="85%">
			<tr class="empty">
			<td rowspan="3"><table border="0" width="79" height="80" cellspacing="0" cellpadding="0"><tr><td height='80' class='avatar'><?= find_avatar($user['avatar']) ?></td></tr></table></td>
			<td><?= $language['Profile view desc23'] ?> gif, jpg, jpeg, pjpeg, png, x-png<br><?= ceil($config['avatar_size'] / 1024) ?> KB max<br><?= 80 ?> x <?= 80 ?> max</div></td>
			</tr>
			<tr>
			<td><a href="javascript:ShowOrHide('avatar','')"><img src="templates/<?= $template ?>/images/buttons/avatar.gif" border="0" width="18" height="18" alt="<?= $language['Profile avatar desc3'] ?>"></a><?= $del ?></td>
			</tr>
			</table>
			</td>
			</tr>
			<input type="hidden" name="a" value="2">
			</table>
			</form>
			<span id="avatar" style="display:none;">
			<br>
			<form method="post" enctype="multipart/form-data" action="<?= $PHP_SELF ?>?n=modules/panel" id="upload_avatar">
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<tr>
			<td width=50% class="con1" align="left"><input name="req_file" class="box" type="file" size="40"></td>
			<td width=50% class="con1" align="right"><input type="submit" name="upload" style="width:100" class="box" value="<?= $language['Profile upload'] ?>"></td>
			</tr>
			<input type="hidden" name="a" value="3">
			<input type="hidden" name="d" value="<?= $user['id'] ?>">
			</table>
			</form>
			</span>
		<?

	break;

	//-----------------------------------------------------------------
	//  Edit profile case
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  First thing's first
		//-----------------------------------------------------------------

		if($editpassword != $editpassword2){ header("location: $PHP_SELF?n=modules/panel&i=10"); exit; }
		if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $editemail)){ header("location: $PHP_SELF?n=modules/panel&i=12"); exit; }

		//-----------------------------------------------------------------
		//  Make Aditional checks
		//-----------------------------------------------------------------

		if(!$editurl) $editurl = NULL;
		if(!$editflag) $editflag = NULL;
		if(!$editlocation) $editlocation = NULL;
		if($editday == 0 || $editmonth == 0 || $edityear == 0) $editage = NULL;
		else $editage = mktime(0,0,0,$editmonth,$editday,$edityear);
		if(!$editmsn) $editmsn = NULL;
		if(!$edityahoo) $edityahoo = NULL;
		if(!$editicq) $editicq = NULL;
		if(!$editaim) $editaim = NULL;
		if(!$editskype) $editskype = NULL;
		if(!$editsignature) $editsignature = NULL;
		if(!$editaboutme) $editaboutme = NULL;

		//-----------------------------------------------------------------
		//  BBcode check
		//-----------------------------------------------------------------

		for ($i = 1; $i <= 5; $i++)
		{
			if(check_tag_order($editsignature) == $i) { header("location: $PHP_SELF?n=modules/panel&i=9$i"); exit; }
			if(check_tag_order($editaboutme) == $i) { header("location: $PHP_SELF?n=modules/panel&i=9$i"); exit; }
		}

		//-----------------------------------------------------------------
		//  Make Password check
		//-----------------------------------------------------------------

		if(!$editpassword) $tmp['password'] = $user['password'];
		else $tmp['password'] = md5($editpassword);

		//-----------------------------------------------------------------
		//  Take care of some things
		//-----------------------------------------------------------------

		$editsignature = jcm_linebreaks(trim(jcm_stripslashes($editsignature)));
		$editaboutme = jcm_linebreaks(trim(jcm_stripslashes($editaboutme)));
		if($overflow = check_tag_order($editsignature)) $editsignature = substr($editsignature, 0, $overflow[0]).substr($editsignature, $overflow[1], (strlen($editsignature) - $overflow[0]));
		if($overflow = check_tag_order($editaboutme)) $editaboutme = substr($editaboutme, 0, $overflow[0]).substr($editaboutme, $overflow[1], (strlen($editaboutme) - $overflow[0]));

		//-----------------------------------------------------------------
		//  Write info in database
		//-----------------------------------------------------------------

		$tmp['email'] = $editemail;
		$tmp['url'] = $editurl;
		$tmp['flag'] = $editflag;
		$tmp['location'] = $editlocation;
		$tmp['age'] = $editage;
		$tmp['hideemail'] = $edithideemail;
		$tmp['calendarbday'] = $editcalendarbday;
		$tmp['msn'] = $editmsn;
		$tmp['yahoo'] = $edityahoo;
		$tmp['icq'] = $editicq;
		$tmp['aim'] = $editaim;
		$tmp['skype'] = $editskype;
		$tmp['signature'] = $editsignature;
		$tmp['aboutme']= $editaboutme;

		$db->updateRow("users",$tmp,"id={$user['id']}");

		//-----------------------------------------------------------------
		//  Add/Delete bday to event if 1 was selected
		//-----------------------------------------------------------------

		if($editcalendarbday == 1 && $editday != 0 && $editmonth != 0)
		{
			$get_id = $db->getLine("SELECT * FROM events WHERE type = 'bday' ".
                                   "AND user_id = '{$user['id']}' AND ".
                                   "user_user = '{$user['username']}'");

			if ($get_id)
			{
				$event_edit['day'] = $editday;
				$event_edit['month'] = $editmonth;

				$db->updateRow("events", $event_edit, "id = {$get_id['id']}");
			}
			else
			{
				$event_add['type'] = "bday";
				$event_add['user_id'] = $user['id'];
				$event_add['user_user'] = $user['username'];
				$event_add['day'] = $editday;
				$event_add['month'] = $editmonth;
				$event_add['year'] = date("Y");
				$event_add['message'] = $user['username'].
                                        $language['Register birthday'];

				$db->insertRow("events", $event_add);
			}
		}
		else
			$db->deleteRow("events", "type = 'bday' AND user_id='{$user['id']}".
                           "' AND user_user='{$user['username']}'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/panel&i=26");

	break;

	//-----------------------------------------------------------------
	//  Upload avatar case
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Define variable for easier manipulation
		//-----------------------------------------------------------------

		$uploaded_file = $_FILES['req_file'];

		//-----------------------------------------------------------------
		//  Make sure the upload went smooth
		//-----------------------------------------------------------------

		switch ($uploaded_file['error'])
		{
			case 1:
			case 2: header("location: $PHP_SELF?n=modules/panel&i=27"); exit; break;
			case 3: header("location: $PHP_SELF?n=modules/panel&i=28"); exit; break;
			case 4: header("location: $PHP_SELF?n=modules/panel&i=29"); exit; break;
			default: if ($uploaded_file['size'] == 0) { header("location: $PHP_SELF?n=modules/panel&i=29"); exit; } break; 
		}

		//-----------------------------------------------------------------
		//  If avatar is uploaded
		//-----------------------------------------------------------------

		if (is_uploaded_file($uploaded_file['tmp_name']))
		{
			//-----------------------------------------------------------------
			//  Define allowed types of avatar and Find dimmensions
			//-----------------------------------------------------------------

			$allowed_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
			list($width, $height, ,) = getimagesize($uploaded_file['tmp_name']);

			//-----------------------------------------------------------------
			//  Do some checks
			//-----------------------------------------------------------------

			if (!in_array($uploaded_file['type'], $allowed_types)) { header("location: $PHP_SELF?n=modules/panel&i=30"); exit; }
			if ($width > 80){ header("location: $PHP_SELF?n=modules/panel&i=31"); exit; }
			if ($height > 80) { header("location: $PHP_SELF?n=modules/panel&i=32"); exit; }
			if ($uploaded_file['size'] > $config['avatar_size']) { header("location: $PHP_SELF?n=modules/panel&i=33"); exit; }

			//-----------------------------------------------------------------
			//  If avatar is gif upload it
			//-----------------------------------------------------------------

			if ($uploaded_file['type'] == 'image/gif')
			{
				$temp = @move_uploaded_file($uploaded_file['tmp_name'], images.'/'.avatars.'/'.$d.'.gif');

				@chmod(images.'/'.avatars.'/'.$d.'.gif', 0644);
				@unlink(images.'/'.avatars.'/'.$d.'.jpg');
				@unlink(images.'/'.avatars.'/'.$d.'.png');

				$tmp['avatar'] = "{$user['id']}.gif";
				$db->updateRow("users",$tmp,"id={$user['id']}");
			}

			//-----------------------------------------------------------------
			//  If avatar is jpg upload it
			//-----------------------------------------------------------------

			else if ($uploaded_file['type'] == 'image/jpeg' || $uploaded_file['type'] == 'image/pjpeg')
			{
				$temp = @move_uploaded_file($uploaded_file['tmp_name'], images.'/'.avatars.'/'.$d.'.jpg');

				@chmod(images.'/'.avatars.'/'.$d.'.jpg', 0644);
				@unlink(images.'/'.avatars.'/'.$d.'.gif');
				@unlink(images.'/'.avatars.'/'.$d.'.png');

				$tmp['avatar'] = "{$user['id']}.jpg";
				$db->updateRow("users",$tmp,"id={$user['id']}");
			}

			//-----------------------------------------------------------------
			//  If avatar is png upload it
			//-----------------------------------------------------------------

			else if ($uploaded_file['type'] == 'image/png' || $uploaded_file['type'] == 'image/x-png')
			{
				$temp = @move_uploaded_file($uploaded_file['tmp_name'], images.'/'.avatars.'/'.$d.'.png');

				@chmod(images.'/'.avatars.'/'.$d.'.png', 0644);
				@unlink(images.'/'.avatars.'/'.$d.'.gif');
				@unlink(images.'/'.avatars.'/'.$d.'.jpg');

				$tmp['avatar'] = "{$user['id']}.png";
				$db->updateRow("users",$tmp,"id={$user['id']}");
			}

			//-----------------------------------------------------------------
			//  If the upload goes bad redirect with error
			//-----------------------------------------------------------------

			if(!$temp) header("location: $PHP_SELF?n=modules/panel&i=28");
		}

		//-----------------------------------------------------------------
		//  If avatar is not uploaded show unknown error
		//-----------------------------------------------------------------

		else header("location: $PHP_SELF?n=modules/panel&i=34");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/panel&i=24");

	break;

	//-----------------------------------------------------------------
	//  Delete avatar case
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Delete the avatars
		//-----------------------------------------------------------------

		@unlink("images/avatars/{$user['id']}.gif");
		@unlink("images/avatars/{$user['id']}.jpg");
		@unlink("images/avatars/{$user['id']}.png");

		//-----------------------------------------------------------------
		//  Write in database
		//-----------------------------------------------------------------

		$tmp['avatar'] = NULL;
		$db->updateRow("users",$tmp,"id={$user['id']}");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/panel&i=25");

	break;

	//-----------------------------------------------------------------
	//  Default case
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Redirect according location or flag + Trick
		//-----------------------------------------------------------------

		if(!isset($i))
		{
			if(isset($user['flag']) && !isset($user['location']))
				header("location: $PHP_SELF?n=modules/panel&a=1&o=1");
			else
				header("location: $PHP_SELF?n=modules/panel&a=1");
		}
		else
		{
			if(isset($user['flag']) && !isset($user['location']))
				header("location: $PHP_SELF?n=modules/panel&a=1&o=1&i=$i");
			else
				header("location: $PHP_SELF?n=modules/panel&a=1&i=$i");
		}
}
?>